package vip.sweet.sys.modular.role.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import vip.sweet.aop.annotations.LocalLock;
import vip.sweet.common.annotations.CommonOperationLog;
import vip.sweet.common.enums.BusinessType;
import vip.sweet.common.enums.ResponseEnum;
import vip.sweet.common.response.CommonResponse;
import vip.sweet.sys.modular.role.entity.Role;
import vip.sweet.sys.modular.role.params.AddRoleParams;
import vip.sweet.sys.modular.role.params.RoleGrantResourceParams;
import vip.sweet.sys.modular.role.params.RolePageParam;
import vip.sweet.sys.modular.role.service.RoleService;

/**
 * @author tianlianyin
 * @date 2024/11/11
 */
@Tag(name = "角色管理控制器")
@RequestMapping("/sys/role")
@RestController
@Slf4j
public class RoleController {
    @Resource
    private RoleService roleService;
    @Operation(summary = "添加角色")
    @PostMapping("/add")
    @CommonOperationLog(title = "添加角色",businessType = BusinessType.INSERT)
    @LocalLock(key = "sys:role:add:{#addRoleParams.name}:{#addRoleParams.code}")
    public CommonResponse add(@RequestBody AddRoleParams addRoleParams){
        if (roleService.add(addRoleParams))
            return CommonResponse.success("添加成功");
        return CommonResponse.error(ResponseEnum.ERROR, "添加失败");
    }
    @GetMapping("/delete")
    @CommonOperationLog(title = "删除角色",businessType = BusinessType.DELETE)
    @Operation(summary = "删除角色")
    public CommonResponse delete(@RequestParam("id") String id){
        if (roleService.delete(id))
            return CommonResponse.success("删除成功");
        return CommonResponse.error(ResponseEnum.ERROR, "删除失败,该数据已删除或不存在");
    }
    @Operation(summary = "编辑角色")
    @CommonOperationLog(title = "编辑角色",businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public CommonResponse edit(@RequestBody Role role){
        if (roleService.edit(role))
            return CommonResponse.success("修改成功");
        return CommonResponse.error(ResponseEnum.ERROR, "修改失败");
    }
    @PostMapping("/page")
    @Operation(summary = "获取角色列表")
    public CommonResponse page(@RequestBody RolePageParam pageParam){
        return CommonResponse.success(roleService.page(pageParam));
    }
    @GetMapping("/list")
    @Operation(summary = "获取角色列表")
    public CommonResponse list(){
        return CommonResponse.success(roleService.list());
    }
    @GetMapping("/getResourceById")
    @Operation(summary = "获取角色资源树")
    public CommonResponse getResourceById(@RequestParam("roleId") String roleId){
        return CommonResponse.success(roleService.getAllResourceById(roleId));
    }
    @PostMapping("/grantResource")
    @Operation(summary = "授权角色资源")
    @PreAuthorize("hasAuthority('sys:role:grantResource')")
    @CommonOperationLog(title = "授权角色资源",businessType = BusinessType.GRANT)
    public CommonResponse grantResource(@RequestBody RoleGrantResourceParams roleGrantResourceParams){
        return roleService.grantResource(roleGrantResourceParams)?CommonResponse.success("授权成功"):CommonResponse.error(ResponseEnum.ERROR,  "授权失败,参数错误");
    }
}
